package com.shuqi.activity.viewport;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.HorizontalScrollView;

/**
 * 左右滑动时可取消父类事件的ScrollView
 * 
 * @author linyuren
 * 
 */
public class MyHorizontalScrollView extends HorizontalScrollView {

    private float downX;
    private float mTouchSlop;

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        ViewConfiguration configuration = ViewConfiguration.get(context);
        mTouchSlop = configuration.getScaledTouchSlop();
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        ViewConfiguration configuration = ViewConfiguration.get(context);
        mTouchSlop = configuration.getScaledTouchSlop();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_MOVE:
                if (mTouchSlop > 0 && Math.abs(downX - ev.getX()) > mTouchSlop) {
                    super.onTouchEvent(ev);
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return true;
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

}
